package com.lyh.controller;


import com.lyh.pojo.Condition;
import com.lyh.pojo.Result;
import com.lyh.pojo.Route;
import com.lyh.service.TicketService;
import com.lyh.utils.RegexUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;

    @PostMapping("/queryPage")
    @ResponseBody
    Result<String> queryTicket2(@RequestParam("from") String from, @RequestParam("to")String to,
                                @RequestParam("date")String date, @RequestParam(value = "high",required = false) String high,
                                @RequestParam(value = "high",required = false)String count,
                                @RequestParam(value = "high",required = false) String precise){
//        System.out.printf("%s, %s, %s, %s, %s, %s",from,to,date,high,count,precise);
//        System.out.println("触发并发查询接口");
        if(!RegexUtils.notNull(date))
            return null;

        String s = from;
        if (from.contains(" ")) {
            //只需要中间的市名, 不需要省和区
            s = from.split(" ")[1];
            //不要最后一个市字
            if('市'==s.charAt(s.length()-1))
                s = s.substring(0,s.length()-1);
        }
        //如果没有传入页数, 那么就默认显示第一页
        int countNum = 1;
        if (count!=null&&!"".equals(count))
            countNum = Integer.parseInt(count);
        Result<String> stringResult = ticketService.queryPageLoading(s, to, date, countNum, high, precise);
        System.out.println("-----------------------返回结果-------------------------------");
//        stringResult.getResultList().forEach(System.out::println);
        return stringResult;
    }


    /**
     * 查询车票
     * @param from 出发地
     * @param to 到达地
     * @param date 日期
     * @param high 是否为高速列车
     * @param count 查询页数
     * @param precise 是否精确查询
     * @return 车票json数据
     */
    @RequestMapping("/query")
    @ResponseBody
    Result<String> queryTicket(String from, String to, String date, String high, String count, String precise){
        System.out.printf("%s, %s, %s, %s, %s, %s",from,to,date,high,count,precise);
        if(!RegexUtils.notNull(date))
            return null;

        String s = from;
        if (from.contains(" ")) {
            //只需要中间的市名, 不需要省和区
            s = from.split(" ")[1];
            //不要最后一个市字
            if('市'==s.charAt(s.length()-1))
                s = s.substring(0,s.length()-1);
        }
        //如果没有传入页数, 那么就默认显示第一页
        int countNum = 1;
        if (count!=null&&!"".equals(count))
            countNum = Integer.parseInt(count);
        //调用Service层
        return ticketService.queryRoutesByStation(s,to,date, countNum, high, precise);
    }

    /**
     * 查询筛选条件
     */
    @RequestMapping("/queryCondition")
    @ResponseBody
    Condition queryCondition(String from, String to, String date){

        if(!RegexUtils.notNull(from))
            return null;

        String s = from;

        if(from.contains(" ")){
            s = from.split(" ")[1];
            s = s.substring(0,s.length()-1);
        }
        return ticketService.queryCondition(s, to, date);
    }


}
